<?php
require_once dirname(__FILE__) . '/../common/common.inc.php';
require_once dirname(__FILE__) . "/../database/$database/database.inc.php";

header('Content-Type: text/plain');

global $myManager;

$action = MiscUtils::getParam('action', NULL);

$myManager->start_t();
try {
  switch ($action) {
    case 'login':
      login($myManager);
      break;
  }
} catch (PDOException $e) {
  echo ERROR_SIGN . $e->getMessage();
}
$myManager->commit_t();

function login($myManager) {
  $args = json_decode(MiscUtils::getParam('a', '[]'));
  
  $orm = classToOrm('pz_user');
  $result = $orm->find($myManager, NULL, NULL, NULL, NULL, "d.account = '$args->account' AND d.password = '$args->password'", NULL);
  if (count($result->data) == 1) {
    $user = $result->data[0];
    
    $_SESSION[USER] = $user;
    
    setcookie(USER, json_encode($_SESSION[USER]), (time() + 3600), PATH, DOMAIN);
    
    echo json_encode($user);
  } else {
    echo ERROR_SIGN . 'Incorrect Account/Password';
  }
}
?>